package br.com.ordersys.web.client.modules.uicomp;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class BallonPopup extends PopupPanel {
	
	private SimplePanel contentPanel;
	private HTML arrow;

	static{
		Resources.INSTANCE.ballonPopupStyle().ensureInjected();
	}
	
	public BallonPopup() {
		super(true);
		setStyleName(Resources.INSTANCE.ballonPopupStyle().ballonPopup());

		prepareLayout();
	}

	private void prepareLayout() {
		VerticalPanel vPanel = new VerticalPanel();
		setWidget(vPanel);

		arrow = new HTML();
		
		StringBuffer buf = new StringBuffer();
		buf.append("<div style='width:100%;'><span class='").append(Resources.INSTANCE.ballonPopupStyle().ballonPopupTail()).append(" ").append(Resources.INSTANCE.ballonPopupStyle().ballonPopupTail_m1()).append("'>");
		buf.append("</span><span class='").append(Resources.INSTANCE.ballonPopupStyle().ballonPopupTail()).append(" ").append(Resources.INSTANCE.ballonPopupStyle().ballonPopupTail_m2()).append("'></span></div>");
		
		arrow.setHTML(buf.toString());
		
		vPanel.add(arrow);
		vPanel.setCellHeight(arrow, "20px");
		
		contentPanel = new SimplePanel();
		contentPanel.setStyleName(Resources.INSTANCE.ballonPopupStyle().ballonPopupBorderPanel());

		vPanel.add(contentPanel);
	}

	public void addContentWidget(Widget w) {
		contentPanel.setWidget(w);
	}

	public void showPopUpRelativeTo(final Widget w) {

		setPopupPositionAndShow(new PositionCallback() {
			@Override
			public void setPosition(int width, int height) {
				int maxX = Window.getClientWidth() - 20;
				int actualHeight = w.getOffsetHeight();
				int actualWidth = w.getOffsetWidth();

				int ADJUST = 0;
				int ARROW_WIDTH = 20; 

				int calculatedTop = (w.getAbsoluteTop() + actualHeight) + 5;

				int centerX = w.getAbsoluteLeft() + (actualWidth / 2); // achamos o meio do componente relativo
				int calculatedLeft = centerX - ((width + ADJUST) / 2);

				int calculatedRight = calculatedLeft + width + ADJUST;

				if (calculatedRight > maxX) {
					calculatedLeft -= (calculatedRight - maxX) + ADJUST;
				}
				
				if(calculatedLeft <= 0){
					calculatedLeft = 4;
				}

				arrow.getElement().getStyle().setPosition(Position.ABSOLUTE);

				int arrowLeft = (centerX - calculatedLeft - (ARROW_WIDTH / 2));
				int arrowMaxLeft = width - (ARROW_WIDTH * 2);

				if (arrowLeft > arrowMaxLeft) {
					arrowLeft = arrowMaxLeft;
				}

				arrow.getElement().getStyle().setLeft(arrowLeft, Unit.PX);

				setPopupPosition(calculatedLeft, calculatedTop);
			}
		});
	}
	
	public interface Resources extends ClientBundle{
		Resources INSTANCE = GWT.create(Resources.class);
		
		@Source("br/com/ordersys/web/client/modules/main/css/BallonPopup.css")
		BallonPopupStyle ballonPopupStyle();
	}
	
	public interface BallonPopupStyle extends CssResource {

		String ballonPopupTail_m1();

		String ballonPopupTail_m2();

		String ballonPopup();

		String ballonPopupTail();

		String ballonPopupBorderPanel();
	}

}
